﻿<Window x:Class="MineSweeper.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:conv="clr-namespace:CommonUtilities.Converters;assembly=CommonUtilities"
        Title="MainWindow" Height="350" Width="625">
    <Window.Resources>
        <conv:VisibilityConverter x:Key="VisibilityConverter" />
    </Window.Resources>

    <Grid Name="grdMain" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <UniformGrid Name="uniControls" Rows="1">
                <Label Name="lblRows" HorizontalAlignment="Right" Content="Rows:" />
                <ComboBox Name="cmbRows" HorizontalAlignment="Left" VerticalAlignment="Center" Width="50" ItemsSource="{Binding RowValues}" SelectedValue="{Binding RowCount}" />
                <Label Name="lblColumns" HorizontalAlignment="Right" Content="Colums:" />
                <ComboBox Name="cmbColumns" HorizontalAlignment="Left" VerticalAlignment="Center" Width="50" ItemsSource="{Binding RowValues}" SelectedValue="{Binding ColumnCount}" />
                <Button Name="btnReset" Width="{Binding ElementName=btnReset, Path=ActualHeight}" HorizontalAlignment="Center" VerticalAlignment="Center" Content=": )" Command="{Binding RefreshCodeCommand}" />
            </UniformGrid>

            <UniformGrid Name="uniCounters" Rows="1">
                <Label Name="lblBlocks" HorizontalAlignment="Right" Content="Blocks:" />
                <TextBlock Name="txtbBlockCount" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding BlockCount}" />
                <Label Name="lblMines" HorizontalAlignment="Right" Content="Mines:" />
                <TextBlock Name="txtbMines" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding MineCount}" />
            </UniformGrid>
        </StackPanel>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Label Name="lblElapsed" HorizontalAlignment="Right" Content="Elapsed:" />
            <TextBlock Name="txtbElapsed" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding ElapsedTime}" />
        </StackPanel>

        <ItemsControl Name="itmctrlBlocks" Grid.Row="1" ItemsSource="{Binding Blocks}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Name="uniBlocksArrangement" Rows="{Binding GridRowCount}" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Name="btnBlocks" IsEnabled="{Binding IsEnabled}" Background="{Binding BlockBackground}"
                            Command="{Binding DataContext.BlockClickCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
                            CommandParameter="{Binding }">
                        <i:Interaction.Triggers>
                            <i:EventTrigger>
                                <i:InvokeCommandAction CommandName="" Command="{Binding MiddleClickCommand}" CommandParameter="{Binding }" />
                            </i:EventTrigger>
                            <i:EventTrigger>
                                <i:InvokeCommandAction CommandName="" Command="{Binding RightClickCommand}" CommandParameter="{Binding }" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <Button.Content>
                            <StackPanel Orientation="Vertical">
                                <StackPanel Orientation="Horizontal">
                                    <RadioButton Content="B" IsChecked="{Binding MarkedBomb}">
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Checked">
                                                <i:InvokeCommandAction Command="{Binding DataContext.FlagCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
                                                                       CommandParameter="{Binding }" />
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="Unchecked">
                                                <i:InvokeCommandAction Command="{Binding DataContext.FlagCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
                                                                       CommandParameter="{Binding }" />
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </RadioButton>
                                    <RadioButton Content="Safe" IsChecked="{Binding MarkedSafe}" />
                                </StackPanel>
                                <UniformGrid HorizontalAlignment="Center" VerticalAlignment="Center" Rows="1">
                                    <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                                               Text="{Binding AdjMineCount}" Visibility="{Binding IsMine, Converter={StaticResource VisibilityConverter}, ConverterParameter=-}" />
                                    <TextBlock Text="{Binding }" />
                                </UniformGrid>
                            </StackPanel>
                        </Button.Content>
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>
